package an.example;

import java.util.Map;

import an.chopsticks.provider.Authenticator;
import an.chopsticks.service.Attribute;
import an.chopsticks.service.AttributeType;
import an.chopsticks.service.AuthenticationFailedException;
import an.chopsticks.service.AuthenticationFlag;
import an.chopsticks.service.Context;
import an.chopsticks.service.Credential;
import an.chopsticks.service.DefaultContextImpl;
import an.chopsticks.service.Service;
import an.chopsticks.service.Subject;
import an.config.ConfigElement;
import an.control.OperationFailedException;
import an.control.Status;

public class FooAuthenticator implements Authenticator {
    public FooAuthenticator(Service service, ConfigElement config) {}

    @SuppressWarnings("unchecked")
    public Context authenticate(Credential cred, Context context) throws AuthenticationFailedException {
        if (cred.getType().equals(Credential.TYPE_ATN_NAMEPASS)) {
            // Get the credential object
            Map<String, Object> namePass = (Map<String, Object>)cred.getCredentialObject();
            // Retrieve information from it
            String userName = (String)namePass.get(Credential.KEY_USERNAME);
            if (userName.equalsIgnoreCase("foo")) {
                Context response = new DefaultContextImpl();
                Attribute subjAttr = new Attribute(Subject.ATTR_SUBJECT, AttributeType.String, userName);
                response.setAttribute(subjAttr);
                return response;
            }
        }
        throw new AuthenticationFailedException("Failed");
    }

    public AuthenticationFlag getFlag() {
        return AuthenticationFlag.Required;
    }

    public String getSupportedCredentialType() {
        return Credential.TYPE_ATN_NAMEPASS;
    }

    public String getProviderName() {
        return "FooAuthenticator";
    }

    public Status getStatus() {
        // TODO Auto-generated method stub
        return null;
    }

    public Object getStatusProperty(String key) {
        // TODO Auto-generated method stub
        return null;
    }

    public void pause() throws OperationFailedException {
        // TODO Auto-generated method stub

    }

    public void resume() throws OperationFailedException {
        // TODO Auto-generated method stub

    }

    public void shutdown() throws OperationFailedException {
        // TODO Auto-generated method stub

    }

    public void shutdownForce() throws OperationFailedException {
        // TODO Auto-generated method stub

    }

    public void start() throws OperationFailedException {
        // TODO Auto-generated method stub

    }
}